<template>
  <div>
    <div class="tools-div">
      <el-button type="success" size="small" @click="dialogVisible = true">
        添 加
      </el-button>
    </div>

    <el-table :data="list" style="width: 100%">
      <el-table-column prop="name" label="品牌名称" />
      <el-table-column prop="logo" label="品牌图标" #default="scope">
        <img :src="scope.row.logo" width="50" />
      </el-table-column>
      <el-table-column prop="createTime" label="创建时间" />
      <el-table-column label="操作" align="center" width="200" #default="scope">
        <el-button
          type="primary"
          size="small"
          @click="handleShowEditDialog(scope.row)"
        >
          修改
        </el-button>
        <el-button type="danger" size="small" @click="handleDelete(scope.row)">
          删除
        </el-button>
      </el-table-column>
    </el-table>
    <div style="margin-top: 80px;">
      <el-pagination
        v-model:current-page="pageNum"
        v-model:page-size="pageSize"
        :page-sizes="[1, 2, 3, 4, 5]"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="fetchData"
        @current-change="fetchData"
      />
    </div>
    <!-- 新增修改品牌的对话框 -->
    <div>
      <el-dialog v-model="dialogVisible" title="添加或修改" width="30%">
        <el-form label-width="120px">
          <el-form-item label="品牌名称">
            <el-input v-model="brand.name" />
          </el-form-item>
          <el-form-item label="品牌图标">
            <el-upload
              class="avatar-uploader"
              :action="uploadPath"
              :headers="headers"
              :show-file-list="false"
              :on-success="handleUploadSuccess"
            >
              <img v-if="brand.logo" :src="brand.logo" class="avatar" />
              <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
            </el-upload>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="handleSaveOrUpdate">
              提交
            </el-button>
            <el-button @click="dialogVisible = false">取消</el-button>
          </el-form-item>
        </el-form>
      </el-dialog>
    </div>
  </div>
</template>
<script setup>
import { ref, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { useApp } from '@/pinia/modules/app'
import {
  GetBrandPage,
  DeleteBrandById,
  GetBrandById,
  UpdateBrandById,
  SaveBrand,
} from '@/api/brand'

const list = ref([])
const pageNum = ref(1)
const pageSize = ref(3)
const total = ref(0)
const fetchData = async () => {
  console.log(111)
  const { data } = await GetBrandPage(pageNum.value, pageSize.value)
  list.value = data.list
  total.value = +data.total
}
onMounted(() => {
  fetchData()
})
//对话框的属性和函数
const brand = ref({})
const dialogVisible = ref(false)
const uploadPath = 'http://localhost:8501/admin/file/upload?module=logo'
const headers = { token: useApp().authorization.token }
const handleUploadSuccess = response => {
  //上传成功的回调函数
  if (+response.code === 200) {
    brand.value.logo = response.data //接收返回的上传的图片地址
    ElMessage.success('上传成功')
  } else {
    ElMessage.success(response.message)
  }
  // dialogVisible.value = false
}
const handleSaveOrUpdate = () => {
  if (brand.value.id) {
    //更新
    handleUpdate()
  } else {
    //新增
    handleSave()
  }
}
const handleSave = async () => {
  await SaveBrand(brand.value)
  ElMessage.success('新增成功')
  fetchData()
  dialogVisible.value = false
  brand.value = {}
}
//修改
//弹出修改对话框
const handleShowEditDialog = row => {
  //直接使用row回显
  // brand.value = row //对象浅拷贝：  row 和brand指向同一个对象
  //如果修改数据时希望互不影响，可以使用对象的深拷贝: 对象转json再转对象、 前端提供的...也可以
  brand.value = { ...row }
  dialogVisible.value = true
}
const handleUpdate = async () => {
  await UpdateBrandById(brand.value)
  ElMessage.success('更新成功')
  fetchData()
  dialogVisible.value = false
  brand.value = {}
}
//删除
const handleDelete = async row => {
  await DeleteBrandById(row.id)

  ElMessage.success('删除成功')
  fetchData()
}
</script>
<style>
.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  text-align: center;
}
</style>
